<?php
Loader::loadFile('config.php', "modules/InnoForum");

Loader::loadClass('DateUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('ObjectUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('PNObjectEx', "modules/InnoForum/pnincludes");
Loader::loadClass('PNObjectExArray', "modules/InnoForum/pnincludes");
Loader::loadClass('DBUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('SecurityUtilEx', "modules/InnoForum/pnincludes");
Loader::loadClass('InnoUtil', "modules/InnoForum/pnincludes");

/**
 * initialise block
 * 
 * @author       The PostNuke Development Team
 */
function InnoForum_RandomVIPCardblock_init()
{

}

/**
 * get information on block
 * 
 * @author       The PostNuke Development Team
 * @return       array       The block information
 */
function InnoForum_RandomVIPCardblock_info()
{
    return array('text_type'      => 'Random VIP Card',
                 'module'         => 'InnoForum',
                 'text_type_long' => 'Random VIP Card',
                 'allow_multiple' => true,
                 'form_content'   => false,
                 'form_refresh'   => false,
                 'show_preview'   => true);
}

/**
 * display block
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the rendered bock
 */
function InnoForum_RandomVIPCardblock_display($blockinfo)
{
    $modname  = 'InnoForum';
//    $class    = 'RandomVIPCard';

//    $id    = FormUtil::getPassedValue ('id', NULL , 'GET');

//    $vars = pnBlockVarsFromContent($blockinfo['content']);
//    $samesize = isset($vars['pagesize'])?$vars['pagesize']:20;

    $render = pnRender::getInstance($modname);
    
    $objectArray = array();

    //////////////////////////////////////
    //Query this topics information
    //////////////////////////////////////
    $pntables = pnDBGetTables();

    //Query random uid 
    $tableGroupMembership  = $pntables['group_membership'];
    $columnGroupMembership = $pntables['group_membership_column'];
    $queryGroupMembership = "SELECT
                                $tableGroupMembership.$columnGroupMembership[uid]
                              FROM
                                $tableGroupMembership
                              WHERE
                                $tableGroupMembership.$columnGroupMembership[gid] = 5
                              OR
                                $tableGroupMembership.$columnGroupMembership[gid] = 6
                              ORDER BY 
                                RAND() 
                              LIMIT 1
                        ";
    $columnGroupMembership = array("uid");
    $resultGroupMembership = DBUtil::executeSQL($queryGroupMembership);
    $uidArray = DBUtil::marshallObjects ($resultGroupMembership, $columnGroupMembership);
    $uid = $uidArray['0']['uid'];


    //Query get user information
/*
    $tableUsers  = $pntables['users'];
    $columnUsers = $pntables['users_column'];
    $userInfoQuery = "SELECT
                          $tableUsers.$columnUsers[uname],
                          $tableUsers.$columnUsers[email]
                        FROM
                          $tableUsers
                        WHERE
                          $tableUsers.$columnUsers[uid] = ".$uid."
                      ";
*/

    //Query get user information
    $tableUserData  = $pntables['user_data'];
    $columnUserData = $pntables['user_data_column'];

    $tableUserProperty = $pntables['user_property'];
    $columnUserProperty = $pntables['user_property_column'];


    $queryUserData = "SELECT
                            $tableUserProperty.$columnUserProperty[prop_label],
                            $tableUserData.$columnUserData[uda_value]
                          FROM
                            $tableUserData, $tableUserProperty
                          WHERE
                            $tableUserData.$columnUserData[uda_uid] = ".$uid."
                          AND
                            $tableUserData.$columnUserData[uda_propid] = $tableUserProperty.$columnUserProperty[prop_id]
                        ";
    $columnUserData = array("label", "value");
    $resultUserData = DBUtil::executeSQL($queryUserData);
    $userDataArray = DBUtil::marshallObjects ($resultUserData, $columnUserData);

    //print_r($userDataArray[0]["label"]); 
/*
    for($i=0; $i<count($userDataArray); $i++){
      $key = $userDataArray[$i]["label"];
      $objectArray[$i][$key] = $userDataArray[$i]["value"];
    }

    print_r($objectArray); exit;
*/
    //print_r($objectArray); exit;
    $render->assign('objectArray', $userDataArray);

    $blockinfo['content'] = $render->fetch('block_randomvipcard.htm');
    return themesideblock($blockinfo);
}


/**
 * modify block settings
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the bock form
 */
function InnoForum_RandomVIPCardblock_modify($blockinfo)
{
  $vars = pnBlockVarsFromContent($blockinfo['content']);
  //load render
  $render = pnRender::getInstance('InnoForum');
  $render->assign('form', $vars);
  
	return $render->fetch('block_randomvipcard_modify.htm');
}


/**
 * update block settings
 * 
 * @author       The PostNuke Development Team
 * @param        array       $blockinfo     a blockinfo structure
 * @return       $blockinfo  the modified blockinfo structure
 */
function InnoForum_RandomVIPCardblock_update($blockinfo)
{
    
    // Get current content
    $vars = pnBlockVarsFromContent($blockinfo['content']);
    
    // alter the corresponding variable
    $form = FormUtil::getPassedValue ('form', false );
    $vars['pagesize'] = !empty($form['pagesize']) ? $form['pagesize'] : 10;

    // write back the new contents
    $blockinfo['content'] = pnBlockVarsToContent($vars);

    // clear the block cache
    $pnRender = pnRender::getInstance('InnoForum');
    $pnRender->clear_cache('block_randomvipcard.htm');
    
    return $blockinfo;
}
